var params = {
    "enumCategoryId": "",
    "farmName": "",
    "index": 1,
    "size": 10
};
$(function () {
    "use strict";
    loadFarm();
    _category();
});
var totalSum=0;
function loadFarm() {
    $.ajax({
        type: "post",
        url: "/industrial/farm/farmInfo",
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (result) {
            if (result.data.length > 0) {
                var totalCount = result.totalPages;
                totalPage = totalCount;
                $("#total-page").text(totalPage);

                $("#pagination-bar").show();
                $("#pagination-box").html('<ul id="pagination" class="pagination"></ul>');
                $('#pagination').twbsPagination({
                    totalPages: totalCount,
                    visiblePages: 5,
                    first: '第一页',
                    prev: '上一页',
                    next: '下一页',
                    last: '最后页',
                    onPageClick: function(event,page) {
                        var emp = page;
                        params.index = emp;
                        $.ajax({
                                type: "POST",
                                url: "/industrial/farm/farmInfo",
                                data: JSON.stringify(params),
                                contentType: "application/json",
                                success: function (result) {
                                    totalSum = result.totalPages;
                                    var d = result.data;
                                    //渲染数据
                                    $.templates("#farm-list-tmpl").link("#farm-list-data", d);
                                    totalPages = result.totalPages;
                                    $('#farm-count').text(result.totalCount);
                                    $('#page-total').text(result.totalPages);
                                    $('#page-index').val(page);
                                },
                                error: function () {
                                }
                            });
                    },
                    startPage: params.index
                });
            } else {
                $("#farm-list-data").html("<tr><td colspan='8'><h1 align='center' height='300px'>暂无数据</h1></td></tr>");
                $("#pagination-bar").hide();
            }
        }
    });
}

function comments() {
    params.farmName = $("#name").val().trim();
    var catName = $("#cate").val();
    if (catName != "100000000000") {
        params.enumCategoryId = catName;
    }else{
        params.enumCategoryId = "";
    }
    loadFarm();
}


function _category() {
    $.ajax({
        type: "get",
        url: "industrial/category/farmCategoryList",
        dataType: "json",
        async: true,
        success: function (data) {
            $("#cate").html($.templates("#categoryTemp").render(data));
        }
    });
}

$('#go-index').click(function () {
    var index = $('#page-index').val();
    if(index>totalSum){
        index = totalSum;
    }
    params.index = parseInt(index);
    loadFarm();
});
















